//
//  BuyCarDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "BuyCarDetViewController.h"

#import "CarDetView.h"

#import "PinJiaCell.h"
#import "CarFourCell.h"
#import "CarDaiViewController.h"
@interface BuyCarDetViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    UIView *view;//导航栏
    UILabel *labTit;//导航栏标题
    UIButton *btnBack;//返回按钮
    UILabel *navline;//导航栏线
    UILabel *labLine;//红线
    NSMutableArray *datas;
    NSMutableDictionary *op;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *dic;
@property (nonatomic,strong)UIView *views;//table头视图

@end

@implementation BuyCarDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBGColor];
    
    _dic = [NSMutableDictionary dictionary];
    op = [NSMutableDictionary dictionary];
    datas = [NSMutableArray array];
    [_dic setObject:@"aaa" forKey:@"tag"];
    
    [self loadData];
    [self loadData2];
}
//自定义导航栏
- (void)loadNav{
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.0;
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    labTit = [[UILabel alloc] init];
    labTit.alpha = 0.0;
    labTit.text = @"详情";
    labTit.textColor = [UIColor blackColor];
    labTit.font = [UIFont systemFontOfSize:18.0];
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    navline = [[UILabel alloc] init];
    navline.backgroundColor = [UIColor lightGrayColor];
    navline.hidden = YES;
    [self.view addSubview:navline];
    [navline makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateNormal];
    [btnBack setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateHighlighted];
    [btnBack addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnBack];
    [btnBack makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labTit.centerY);
        make.left.equalTo(10);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma -mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>64) {
        view.alpha = 1.0;
        labTit.alpha = 1.0;
        navline.hidden = NO;
        [btnBack setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
        [btnBack setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    }else{
        view.alpha = 0;
        labTit.alpha = 0;
        navline.hidden = YES;
        [btnBack setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateNormal];
        [btnBack setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateHighlighted];
    }
    
}

- (void)loadUIS{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    [self.view addSubview:v];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //底部视图
    UIView *views = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60.0)];
    views.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:views];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [views addSubview:line];
    //
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 10, ScreenWidth/3, 50.0);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"联系商家" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickTel) forControlEvents:UIControlEventTouchUpInside];
    [views addSubview:btn];
    
    //品牌
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = [op objectForKey:@"brand"];
    lab1.textColor = COLOR_RGB(51, 51, 51, 1);
    lab1.font = [UIFont systemFontOfSize:13.0];
    [views addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(8);
        make.top.equalTo(views.top).offset(18);
    }];
    //地址
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.text = [op objectForKey:@"address"];
    lab2.textColor = [UIColor darkGrayColor];
    lab2.font = [UIFont systemFontOfSize:11.0];
    [views addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(views.left).offset(8);
        make.top.equalTo(lab1.bottom).offset(8);
    }];
    
    CarDetView *detView = [[[NSBundle mainBundle] loadNibNamed:@"CarDetView" owner:nil options:nil] lastObject];
    detView.frame = CGRectMake(0, 0, ScreenWidth, 610);
    _tableView.tableHeaderView = detView;
    
    NSString *type = [NSString stringWithFormat:@"%li",[[op objectForKey:@"type"] integerValue]];
    if ([type isEqualToString:@"1"]) {
        detView.labCar.text = @"准新车";
    }else{
        detView.labCar.text = @"准新车";
    }
    detView.labCar.layer.cornerRadius = 5.0;
    detView.labCar.layer.borderWidth = 0.5;
    detView.labCar.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
    
    detView.labTit.text = [op objectForKey:@"brand"];
    NSString *guideprice = [NSString stringWithFormat:@"%.2f万",[[op objectForKey:@"sellingprice"] floatValue]];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
    detView.labPrice1.attributedText = attribtStr;
    
    detView.labPrice2.text = [NSString stringWithFormat:@"%.2f万",[[op objectForKey:@"guideprice"] floatValue]];
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[op objectForKey:@"carthemephoto"]];
    [detView.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"timg"]];
    //
    detView.labColor.text = [op objectForKey:@"carcolor"];
    detView.labStyle.text = detView.labCar.text;
    detView.labYear.text = [NSString stringWithFormat:@"%li年",[[op objectForKey:@"customerservice"] integerValue]];
    detView.labDate.text = [op objectForKey:@"thefactorytime"];
    detView.labSpeed.text = [NSString stringWithFormat:@"%.2f公里",[[op objectForKey:@"travel"] floatValue]];
    detView.labPlace.text = [op objectForKey:@"address"];
    detView.labKM.text = [NSString stringWithFormat:@"%likm",[[op objectForKey:@"km"] integerValue]];
    //
    detView.ViewCD.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickDai)];
    [detView.ViewCD addGestureRecognizer:tap];
    detView.labSFPrice.text = [NSString stringWithFormat:@"%.2f元",[[op objectForKey:@"downpayments"] floatValue]];
    detView.labYGPrice.text = [NSString stringWithFormat:@"%.2f元",[[op objectForKey:@"forthemonth"] floatValue]];
    
}

//联系商家
- (void)clickTel{
    NSString  *phone = [NSString stringWithFormat:@"%li",[[op objectForKey:@"phone"] integerValue]];
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    UIWebView *web = [[UIWebView alloc] init];
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:web];
}
//车贷页面
- (void)clickDai{
    CarDaiViewController *dai = [[CarDaiViewController alloc] init];
    NSString *price = [NSString stringWithFormat:@"%.2f",[[op objectForKey:@"guideprice"] floatValue]];
    dai.labPrice.text = price;
    [self.navigationController pushViewController:dai animated:YES];
}


#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        return 4;
    }else{
        return datas.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        static NSString *cellId = @"Cell";
        CarFourCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CarFourCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else{
        static NSString *cellId = @"Cell";
        PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = datas[indexPath.row];
        cell.labName.text = [dic objectForKey:@"nickname"];
        cell.labText.text = [dic objectForKey:@"comment"];
        cell.labDate.text = [dic objectForKey:@"commenttime"];
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"avatar"]];
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
        
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        return 230;
    }else{
        return 100;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (_views) {
        return _views;
    }
    _views = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, ScreenWidth/2, 45);
    [btn1 setTitle:@"车辆详情" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //15
    [_views addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth/2, 0, ScreenWidth/2, 45);
    [btn2 setTitle:@"评价" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [_views addSubview:btn2];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2, 9, 1, 28)];
    lab.backgroundColor = [UIColor grayColor];
    [_views addSubview:lab];
    
    labLine = [[UILabel alloc] initWithFrame:CGRectMake((ScreenWidth/2-61)/2, 35, 61, 5)];
    labLine.backgroundColor = [UIColor redColor];
    [_views addSubview:labLine];
    
    UILabel *lab3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 44, ScreenWidth, 0.5)];
    lab3.backgroundColor = [UIColor grayColor];
    [_views addSubview:lab3];
    
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
    
    return _views;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45.0;
}

//车辆详情事件
- (void)click1{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake((ScreenWidth/2-61)/2, 35, 61, 5);
    }];
    [_dic setObject:@"aaa" forKey:@"tag"];
    [_tableView reloadData];
}
//评价事件
- (void)click2{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake(ScreenWidth/2+(ScreenWidth/2-30)/2, 35, 30, 5);
    }];
    [_dic setObject:@"sss" forKey:@"tag"];
    [_tableView reloadData];
}

//购车车辆详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_carId forKey:@"id"];
    NSLog(@"paras:%@",parmas);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/CarBuying/details",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"购车车辆详情res:%@",responseObject);
        op = [responseObject objectForKey:@"result"];
        
        [self loadUIS];
        [self loadNav];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}
//购车评论
- (void)loadData2{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_carId forKey:@"merchantid"];
    [parmas setObject:@"1" forKey:@"p"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/buyacar/merchantid/26/p/1",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            datas = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
